package com.ifonly.ar.dialect.db;

import com.ifonly.ar.core.Config;
import com.ifonly.ar.dialect.Dialect;

/**
 * @author ifonly
 * @version 1.0 2015-12-03 21:44
 * @since JDK 1.6
 */
public class MysqlDialect extends Dialect {

    @Override
    public String initTableSql(String tableName) {
        return "SELECT * FROM " + tableName + " WHERE 1 = 2";
    }

    @Override
    public boolean support() {
        return true;
    }

    @Override
    public String sql(String sql, int start, int length) {
        return getLimitSql(sql, start, Config.SQL_PLACEHOLDER,
                Config.SQL_PLACEHOLDER);
    }


    /**
     * 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
     * <pre>
     * 如mysql
     * dialect.getLimitSql("select * from user", 12, ":offset",0,":limit") 将返回
     * select * from user limit :offset,:limit
     * </pre>
     *
     * @param sql               实际SQL语句
     * @param offset            分页开始纪录条数
     * @param offsetPlaceholder 分页开始纪录条数－占位符号
     * @param limitPlaceholder  分页纪录条数占位符号
     * @return 包含占位符的分页sql
     */
    public String getLimitSql(String sql, int offset, String offsetPlaceholder, String limitPlaceholder) {
        return sql + " limit " + offsetPlaceholder + ", " + limitPlaceholder;
    }

}
